package cn.edu.sdjzu.xg.eagle.bysj.controller;

import cn.edu.sdjzu.xg.eagle.bysj.annotation.IgnoreAuth;
import cn.edu.sdjzu.xg.eagle.bysj.annotation.SysLog;
import cn.edu.sdjzu.xg.eagle.bysj.entity.BumenxinxiEntity;

import cn.edu.sdjzu.xg.eagle.bysj.entity.view.BumenxinxiView;
import cn.edu.sdjzu.xg.eagle.bysj.service.Interface.BumenxinxiService;
import cn.edu.sdjzu.xg.eagle.bysj.utils.MPUtil;
import cn.edu.sdjzu.xg.eagle.bysj.utils.PageUtils;
import cn.edu.sdjzu.xg.eagle.bysj.utils.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * 部门信息
 * 后端接口
 * @author 
 * @email 
 * @date 2025-05-27 14:26:10
 */
@RestController
@RequestMapping("/bumenxinxi")
public class BumenxinxiController {
    @Autowired
    private BumenxinxiService bumenxinxiService;

    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, BumenxinxiEntity bumenxinxi,
                  HttpServletRequest request){
                                    EntityWrapper<BumenxinxiEntity> ew = new EntityWrapper<BumenxinxiEntity>();
                                                                                                                                        
        
        
        PageUtils page = bumenxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, bumenxinxi), params), params));
        return R.ok().put("data", page);
    }

	@IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,BumenxinxiEntity bumenxinxi,
		HttpServletRequest request){
        EntityWrapper<BumenxinxiEntity> ew = new EntityWrapper<BumenxinxiEntity>();

		PageUtils page = bumenxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, bumenxinxi), params), params));
        return R.ok().put("data", page);
    }

    @RequestMapping("/lists")
    public R list( BumenxinxiEntity bumenxinxi){
       	EntityWrapper<BumenxinxiEntity> ew = new EntityWrapper<BumenxinxiEntity>();
      	ew.allEq(MPUtil.allEQMapPre( bumenxinxi, MPUtil.camelToSnake("bumenxinxi")));
        return R.ok().put("data", bumenxinxiService.selectListView(ew));
    }

    @RequestMapping("/query")
    public R query(BumenxinxiEntity bumenxinxi){
        EntityWrapper< BumenxinxiEntity> ew = new EntityWrapper< BumenxinxiEntity>();
 		ew.allEq(MPUtil.allEQMapPre( bumenxinxi, MPUtil.camelToSnake("bumenxinxi")));
		BumenxinxiView bumenxinxiView =  bumenxinxiService.selectView(ew);
		return R.ok("查询部门信息成功").put("data", bumenxinxiView);
    }

    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        BumenxinxiEntity bumenxinxi = bumenxinxiService.selectById(id);
		bumenxinxi = bumenxinxiService.selectView(new EntityWrapper<BumenxinxiEntity>().eq("id", id));
        return R.ok().put("data", bumenxinxi);
    }

	@IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        BumenxinxiEntity bumenxinxi = bumenxinxiService.selectById(id);
		bumenxinxi = bumenxinxiService.selectView(new EntityWrapper<BumenxinxiEntity>().eq("id", id));
        return R.ok().put("data", bumenxinxi);
    }



    @RequestMapping("/save")
    @SysLog("新增部门信息")
    public R save(@RequestBody BumenxinxiEntity bumenxinxi, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(bumenxinxi);
        bumenxinxiService.insert(bumenxinxi);
        return R.ok();
    }

    @SysLog("新增部门信息")
    @RequestMapping("/add")
    public R add(@RequestBody BumenxinxiEntity bumenxinxi, HttpServletRequest request){
    	//ValidatorUtils.validateEntity(bumenxinxi);
        bumenxinxiService.insert(bumenxinxi);
        return R.ok();
    }

    @RequestMapping("/update")
    @Transactional
    @SysLog("修改部门信息")
    public R update(@RequestBody BumenxinxiEntity bumenxinxi, HttpServletRequest request){
        //ValidatorUtils.validateEntity(bumenxinxi);
        bumenxinxiService.updateById(bumenxinxi);//全部更新
        return R.ok();
    }

    @RequestMapping("/delete")
    @SysLog("删除部门信息")
    public R delete(@RequestBody Long[] ids){
        bumenxinxiService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    @RequestMapping("/count")
    public R count(@RequestParam Map<String, Object> params,BumenxinxiEntity bumenxinxi, HttpServletRequest request){
        EntityWrapper<BumenxinxiEntity> ew = new EntityWrapper<BumenxinxiEntity>();
        int count = bumenxinxiService.selectCount(MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, bumenxinxi), params), params));
        return R.ok().put("data", count);
    }
}
